package com.lb.jwitter.action;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblQLDTTXADMDanTocService;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamServiceService;
import com.lb.jwitter.service.TblQLDTTXAHocVien;
import com.lb.jwitter.service.TblQLDTTXAHocVienService;
import com.lb.jwitter.service.TblQLDTTXAHocVienTamDungHocKy;
import com.lb.jwitter.service.TblQLDTTXAHocVienTamDungHocKyService;
import com.lb.jwitter.service.TblQLDTTXALopMonHocHocVien;
import com.lb.jwitter.service.TblQLDTTXALopMonHocHocVienService;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.lb.jwitter.util.Message;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXAHocVienTamDungHocKyAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }

    private String idList; 
    public String getIdList() { return idList; } 
    public void setIdList(String idList) { this.idList = idList; }

    private Message msg; 
    public Message getMsg() { return msg; }

    private Long id; 
    public Long getId() {
       return id;
 }

    public void setId(Long id) {
       this.id = id;
 }

    private int hocVienId; 
    public void setHocVienId(int hocVienId) {
       this.hocVienId = hocVienId;
 }

    public int getHocVienId() {
       return hocVienId;
 }
    private int hocKyId; 
    public void setHocKyId(int hocKyId) {
       this.hocKyId = hocKyId;
 }

    public int getHocKyId() {
       return hocKyId;
 }

    private String lyDo;
    public void setLyDo(String lyDo){
    	this.lyDo = lyDo;
    }
    
    public String getLyDo(){
    	return lyDo;
    }
    
    private boolean qdPDT;
    
    public boolean isQdPDT() {
		return qdPDT;
	}

	public void setQdPDT(boolean qdPDT) {
		this.qdPDT = qdPDT;
	}

	private TblQLDTTXAHocVienTamDungHocKy tblQLDTTXAHocVienTamDungHocKy;
    public TblQLDTTXAHocVienTamDungHocKy getTblQLDTTXAHocVienTamDungHocKy () {
        return this.tblQLDTTXAHocVienTamDungHocKy ;
 }

    private List<TblQLDTTXAHocVienTamDungHocKy> tblQLDTTXAHocVienTamDungHocKys; 
    public List<TblQLDTTXAHocVienTamDungHocKy> getTblQLDTTXAHocVienTamDungHocKys() {
        return this.tblQLDTTXAHocVienTamDungHocKys; 
      } 

    private TblQLDTTXAHocVienTamDungHocKyService tblQLDTTXAHocVienTamDungHocKyService; 

    private List<TblQLDTTXAHocVien> tblQLDTTXAHocViens; 
    public List<TblQLDTTXAHocVien> getTblQLDTTXAHocViens() {
         return this.tblQLDTTXAHocViens; 
    }
    
    private List<TblQLDTTXAHocKyTrongNam> tblQLDTTXAHocKyTrongNams;
    public List<TblQLDTTXAHocKyTrongNam> getTblQLDTTXAHocKyTrongNams(){
    	return this.tblQLDTTXAHocKyTrongNams;
    }
    

    private TblQLDTTXAHocVienService tblQLDTTXAHocVienService;
    private TblQLDTTXAHocKyTrongNamService tblQLDTTXAHocKyTrongNamService;
    private TblQLDTTXALopMonHocHocVienService tblQLDTTXALopMonHocHocVienService;


	private int preUpdate(Message msg, TblQLDTTXAHocVienTamDungHocKy obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postUpdate(Message msg, TblQLDTTXAHocVienTamDungHocKy obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
  public String form() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_HocVien_TamDung_HocKy", "xem")){ 
        return "noprivilege"; 
    } 
           tblQLDTTXAHocVienTamDungHocKyService = (TblQLDTTXAHocVienTamDungHocKyService)context.getBean("tbl_QLDTTXA_HocVien_TamDung_HocKyClient");
           tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient");
       	
       if(pageSize == 0){ 
            pageSize = 20; 
        } 
        if(page == 0){ 
            page =1; 
        } 
        List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
        cons.clear();
        { 
            ConditionReport con = new ConditionReport(); 
            con.setCol("id"); 
            con.setValue1("desc"); 
            con.setOperator("order"); 
            cons.add(con); 
        }
        tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNamService.findtblQLDTTXAHocKyTrongNamByCondition(cons, 0, 0);

        cons.clear();
        { 
            ConditionReport con = new ConditionReport(); 
            con.setCol("hocKyId.id"); 
            con.setValue1("desc"); 
            con.setOperator("order"); 
            cons.add(con); 
        } 
        { 
            ConditionReport con = new ConditionReport(); 
            con.setCol("hocVienId.ten"); 
            con.setValue1("asc"); 
            con.setOperator("order"); 
            cons.add(con); 
        } 
        { 
            ConditionReport con = new ConditionReport(); 
            con.setCol("hocVienId.hoTen"); 
            con.setValue1("asc"); 
            con.setOperator("order"); 
            cons.add(con); 
        } 
        tblQLDTTXAHocVienTamDungHocKys =        tblQLDTTXAHocVienTamDungHocKyService.findtblQLDTTXAHocVienTamDungHocKyByCondition(cons, pageSize, page);
        pages = tblQLDTTXAHocVienTamDungHocKyService.counttblQLDTTXAHocVienTamDungHocKyByCondition(new ArrayList<ConditionReport>())/pageSize + 1; 
      dataString = ""; 
      dataString += "&id="; 
      dataString += "&hocVienId="; 
      dataString += "&hocKyId=";
      dataString += "&lyDo=";
           tblQLDTTXAHocVienService = (TblQLDTTXAHocVienService)context.getBean("tbl_QLDTTXA_HocVienClient"); 
      tblQLDTTXAHocViens = tblQLDTTXAHocVienService.findAlltblQLDTTXAHocVien(0, 0);
      
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
      System.out.println("Loi Form tbl_QLDTTXA_HocVien_TamDung_HocKy Action"); 
     e.printStackTrace(); 
    return Action.ERROR; 
 } 
 } 
  	private int preInsert(Message msg, TblQLDTTXAHocVienTamDungHocKy obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postInsert(Message msg, TblQLDTTXAHocVienTamDungHocKy obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
 public String insert() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_HocVien_TamDung_HocKy", "them")){ 
        return "noprivilege"; 
    }           
    tblQLDTTXAHocVienTamDungHocKyService = (TblQLDTTXAHocVienTamDungHocKyService)context.getBean("tbl_QLDTTXA_HocVien_TamDung_HocKyClient"); 
    	tblQLDTTXAHocVienService = (TblQLDTTXAHocVienService)context.getBean("tbl_QLDTTXA_HocVienClient");
    	tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient");
    	tblQLDTTXALopMonHocHocVienService = (TblQLDTTXALopMonHocHocVienService) context.getBean("tbl_QLDTTXA_LopMonHoc_HocVienClient"); 

    	
        msg = new Message(); 
        tblQLDTTXAHocVienTamDungHocKy = new TblQLDTTXAHocVienTamDungHocKy();
        tblQLDTTXAHocVienTamDungHocKy.setHocVienId(tblQLDTTXAHocVienService.gettblQLDTTXAHocVien(new Long(hocVienId)));
        tblQLDTTXAHocVienTamDungHocKy.setHocKyId(tblQLDTTXAHocKyTrongNamService.gettblQLDTTXAHocKyTrongNam(new Long(hocKyId)));
        tblQLDTTXAHocVienTamDungHocKy.setLyDo(lyDo);
        if(qdPDT){
        	tblQLDTTXAHocVienTamDungHocKy.setQdPDT(1);
        } else {
        	tblQLDTTXAHocVienTamDungHocKy.setQdPDT(0);
        }
       
        preInsert(msg,tblQLDTTXAHocVienTamDungHocKy); 
       if (msg.isPreMessage_b()) 
       { 
           id = tblQLDTTXAHocVienTamDungHocKyService.createtblQLDTTXAHocVienTamDungHocKy(tblQLDTTXAHocVienTamDungHocKy); 
           postInsert(msg, tblQLDTTXAHocVienTamDungHocKy); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 
       tblQLDTTXAHocVienTamDungHocKy.setId(id); 
       
       // tam dung cac lop dang hoc trong hoc ky
       List<ConditionReport> cons = new ArrayList<ConditionReport>(); 

       cons.clear();
   	{
   		ConditionReport con = new ConditionReport(); 
	        con.setCol("hocVienId.id");
	        con.setValue1(tblQLDTTXAHocVienTamDungHocKy.getHocVienId().getId());
	        con.setOperator("eq");
	        cons.add(con);	
   	}
   	{
   		ConditionReport con = new ConditionReport(); 
	        con.setCol("lopMonHocId.hocKyId.id");
	        con.setValue1(tblQLDTTXAHocVienTamDungHocKy.getHocKyId().getId());
	        con.setOperator("eq");
	        cons.add(con);	
   	}
   	List<TblQLDTTXALopMonHocHocVien> lopMonHocHocViens = tblQLDTTXALopMonHocHocVienService.findtblQLDTTXALopMonHocHocVienByCondition(cons, 0, 0);
   	for (TblQLDTTXALopMonHocHocVien tblQLDTTXALopMonHocHocVien : lopMonHocHocViens) {
			tblQLDTTXALopMonHocHocVien.setTrangThai(3);
			tblQLDTTXALopMonHocHocVienService.updatetblQLDTTXALopMonHocHocVien(tblQLDTTXALopMonHocHocVien);
		}
       
       if(pageSize == 0){ 
           pageSize = 20; 
       } 
        if(page == 0){ 
           page =1; 
       } 
        
        cons.clear();
        { 
            ConditionReport con = new ConditionReport(); 
            con.setCol("hocKyId.id"); 
            con.setValue1("desc"); 
            con.setOperator("order"); 
            cons.add(con); 
        } 
        { 
            ConditionReport con = new ConditionReport(); 
            con.setCol("hocVienId.ten"); 
            con.setValue1("asc"); 
            con.setOperator("order"); 
            cons.add(con); 
        } 
        { 
            ConditionReport con = new ConditionReport(); 
            con.setCol("hocVienId.hoTen"); 
            con.setValue1("asc"); 
            con.setOperator("order"); 
            cons.add(con); 
        } 
        tblQLDTTXAHocVienTamDungHocKys =        tblQLDTTXAHocVienTamDungHocKyService.findtblQLDTTXAHocVienTamDungHocKyByCondition(cons, pageSize, page);
        pages = tblQLDTTXAHocVienTamDungHocKyService.counttblQLDTTXAHocVienTamDungHocKyByCondition(new ArrayList<ConditionReport>())/pageSize + 1; 
        dataString = ""; 
        dataString += "&id="; 
        dataString += "&hocVienId="; 
        dataString += "&hocKyId=";
        dataString += "&lyDo=";
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Insert tbl_QLDTTXA_HocVien_TamDung_HocKy Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
 	private int preSearch(Message msg, List<ConditionReport> cons ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postSearch(Message msg, List<ConditionReport> cons ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
 public String search() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_HocVien_TamDung_HocKy", "xem")){ 
        return "noprivilege"; 
    } 
    tblQLDTTXAHocVienTamDungHocKyService = (TblQLDTTXAHocVienTamDungHocKyService)context.getBean("tbl_QLDTTXA_HocVien_TamDung_HocKyClient"); 
    msg = new Message(); 
        List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
    if(id!= null && !id.equals(0)){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("id");
        con.setValue1(new Long(id));
        con.setOperator("eq");
        cons.add(con);	
    } 
    if(hocVienId != 0 ){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("hocVienId.id");
        con.setValue1(new Long(hocVienId));
        con.setOperator("eq");
        cons.add(con);	
    } 
    if(hocKyId != 0 ){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("hocKyId.id");
        con.setValue1(new Long(hocKyId));
        con.setOperator("eq");
        cons.add(con);	
    } 
    { 
        ConditionReport con = new ConditionReport(); 
        con.setCol("hocKyId.id"); 
        con.setValue1("desc"); 
        con.setOperator("order"); 
        cons.add(con); 
    } 
    { 
        ConditionReport con = new ConditionReport(); 
        con.setCol("hocVienId.ten"); 
        con.setValue1("asc"); 
        con.setOperator("order"); 
        cons.add(con); 
    } 
    { 
        ConditionReport con = new ConditionReport(); 
        con.setCol("hocVienId.hoTen"); 
        con.setValue1("asc"); 
        con.setOperator("order"); 
        cons.add(con); 
    } 

        preSearch(msg, cons); 
       if (msg.isPreMessage_b()) 
       { 
           if(pageSize == 0){ 
               pageSize = 20; 
           } 
            if(page == 0){ 
               page =1; 
           } 
           tblQLDTTXAHocVienTamDungHocKys = tblQLDTTXAHocVienTamDungHocKyService.findtblQLDTTXAHocVienTamDungHocKyByCondition(cons,pageSize,page); 
           pages = tblQLDTTXAHocVienTamDungHocKyService.counttblQLDTTXAHocVienTamDungHocKyByCondition(cons)/pageSize + 1; 
           postSearch(msg, cons); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 
      dataString = ""; 
      dataString += "&id=" + id; 
      dataString += "&hocVienId=" + hocVienId; 
      dataString += "&hocKyId=" + hocKyId;
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Seach tbl_QLDTTXA_HocVien_TamDung_HocKy Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 

 public String delete() throws Exception 
 { 
  try{ 	
      Map session = ActionContext.getContext().getSession();
      taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
      if( taiKhoanDangNhap == null) return "noprivilege";
     ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
     tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
     if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_HocVien_TamDung_HocKy", "xoa")){ 
         return "noprivilege"; 
     }          
     tblQLDTTXAHocVienTamDungHocKyService = (TblQLDTTXAHocVienTamDungHocKyService)context.getBean("tbl_QLDTTXA_HocVien_TamDung_HocKyClient"); 
 	tblQLDTTXALopMonHocHocVienService = (TblQLDTTXALopMonHocHocVienService) context.getBean("tbl_QLDTTXA_LopMonHoc_HocVienClient"); 

     msg = new Message(); 
     List<ConditionReport> cons = new ArrayList<ConditionReport>(); 

     
     List<String> ids = new ArrayList<String>(); 
     Collections.addAll(ids, idList.split(",")); 
     for(String id : ids){ 
    	 TblQLDTTXAHocVienTamDungHocKy tdhk = tblQLDTTXAHocVienTamDungHocKyService.gettblQLDTTXAHocVienTamDungHocKy(new Long(id));
    	 cons.clear();
    	   	{
    	   		ConditionReport con = new ConditionReport(); 
    		        con.setCol("hocVienId.id");
    		        con.setValue1(tdhk.getHocVienId().getId());
    		        con.setOperator("eq");
    		        cons.add(con);	
    	   	}
    	   	{
    	   		ConditionReport con = new ConditionReport(); 
    		        con.setCol("lopMonHocId.hocKyId.id");
    		        con.setValue1(tdhk.getHocKyId().getId());
    		        con.setOperator("eq");
    		        cons.add(con);	
    	   	}
    	   	{
    	   		ConditionReport con = new ConditionReport(); 
    		        con.setCol("trangThai");
    		        con.setValue1(3);
    		        con.setOperator("eq");
    		        cons.add(con);	
    	   	}
    	   	List<TblQLDTTXALopMonHocHocVien> lopMonHocHocViens = tblQLDTTXALopMonHocHocVienService.findtblQLDTTXALopMonHocHocVienByCondition(cons, 0, 0);
    	   	for (TblQLDTTXALopMonHocHocVien tblQLDTTXALopMonHocHocVien : lopMonHocHocViens) {
    				tblQLDTTXALopMonHocHocVien.setTrangThai(0);
    				tblQLDTTXALopMonHocHocVienService.updatetblQLDTTXALopMonHocHocVien(tblQLDTTXALopMonHocHocVien);
    			}
     } 
     tblQLDTTXAHocVienTamDungHocKyService.deleteListtblQLDTTXAHocVienTamDungHocKy(ids);
        if(pageSize == 0){ 
            pageSize = 20; 
        } 
         if(page == 0){ 
            page =1; 
        } 
         cons.clear();
         { 
             ConditionReport con = new ConditionReport(); 
             con.setCol("hocKyId.id"); 
             con.setValue1("desc"); 
             con.setOperator("order"); 
             cons.add(con); 
         } 
         { 
             ConditionReport con = new ConditionReport(); 
             con.setCol("hocVienId.ten"); 
             con.setValue1("asc"); 
             con.setOperator("order"); 
             cons.add(con); 
         } 
         { 
             ConditionReport con = new ConditionReport(); 
             con.setCol("hocVienId.hoTen"); 
             con.setValue1("asc"); 
             con.setOperator("order"); 
             cons.add(con); 
         } 
         tblQLDTTXAHocVienTamDungHocKys =        tblQLDTTXAHocVienTamDungHocKyService.findtblQLDTTXAHocVienTamDungHocKyByCondition(cons, pageSize, page);
         pages = tblQLDTTXAHocVienTamDungHocKyService.counttblQLDTTXAHocVienTamDungHocKyByCondition(new ArrayList<ConditionReport>())/pageSize + 1; 
         
         dataString = ""; 
       dataString += "&id="; 
       dataString += "&ten="; 
         return Action.SUCCESS; 
       } 
       catch(Exception e){ 
       System.out.println("Loi Delete tbl_QLDTTXA_HocVien_TamDung_HocKy Action"); 
      e.printStackTrace(); 
     return Action.ERROR; 
  } 
  } 
} 
 
